<?php
namespace App\Common\Cla\EChat;

use App\Common as com;
use App\Common\Cla\EChat\Rooms as thisRooms;

class Rooms extends com\Cla\ClaBase {

	private com\Cla\EChat $eChat;

	private array $roomSids;

	private array $receiveIds = [];

	/**
	 * Rooms constructor.
	 * @param com\Cla\EChat $eChat
	 * @param array $roomSids
	 */
	public function __construct( com\Cla\EChat $eChat, array $roomSids ) {
		$this->eChat = $eChat;
		$this->roomSids = $roomSids;
		//
		parent::__construct();
	}

	public function __destruct(  ) {
		// $this->stopReceives();
	}

	public function getEChat(  ):com\Cla\EChat {
		return $this->eChat;
	}

	/**
	 * @param $data
	 * @return bool
	 */
	public function send( $data ):bool {
		//
		foreach( $this->roomSids as $roomSid ){
			if( is_array( $roomSid ) ){
				$this->eChat->goServer()->send( $roomSid[0], $roomSid[1], $data );
			}else{
				$this->eChat->goServer()->send( $this->getEChat()->getChannelSid(), $roomSid, $data );
			}
		}
		//
		return true;
	}

	/**
	 * @param $data
	 * @param \Closure $replyCallback
	 * @param int|null $receiveTimeoutS
	 * @return bool
	 */
	/*
	public function sendOfReply( $data, \Closure $replyCallback, ?int $receiveTimeoutS = 30 ):bool {
		//
		foreach( $this->roomSids as $roomSid ){
			$replyRoomSid = $this->buildReplyRoomSid();
			//
			$this->eChat->joinRooms( [ $replyRoomSid ] )->receive( $replyCallback, $receiveTimeoutS );
			//
			$this->eChat->goServer()->send( $roomSid, $data, [
				'replyRoomSid' => $replyRoomSid,
			] );
		}
		//
		return true;
	}
*/

	/**
	 * @param \Closure $callback
	 * @param int|null $receiveTimeoutS 接收超时（单位：秒） [null]默认超时 [-1][默认]不超时
	 * @return int[] 接收id
	 */
	/*
	public function receive( \Closure $callback, ?int $receiveTimeoutS = null ):array {
		foreach( $this->roomSids as $roomSid ){
			$this->receiveIds[] = $this->eChat->goServer()->receive( $roomSid, function( \PhpAmqpLib\Message\AMQPMessage $AMQPMsg ) use ( $callback ) {
				$msgObj = thisRooms\Msg::make( $this, $AMQPMsg );
				//
				call_user_func( $callback );
			}, $receiveTimeoutS );
		}
		//
		return $this->receiveIds;
	}
	*/

	/**
	 * 停止接收消息
	 * @param int $receiveId 指定id
	 * @return $this
	 */
	/*
	public function stopReceive( int $receiveId ):self {
		$this->stopReceives( [ $receiveId ] );
		//
		return $this;
	}
	*/

	/**
	 * @param array|null $receiveIds 指定id [null][默认]全部
	 * @return $this
	 */
	/*
	public function stopReceives( ?array $receiveIds = null ):self {
		$stopReceiveIds = $receiveIds === null ? $this->receiveIds : $receiveIds ;
		//
		foreach( $stopReceiveIds as $receiveId ){
			$this->eChat->goServer()->stopReceive( $receiveId );
		}
		// 去掉
		$this->receiveIds = array_diff( $this->receiveIds, $stopReceiveIds );
		//
		return $this;
	}
	*/

	/**
	 * 创建回复专用房间sid
	 * @return string
	 */
	/*
	private function buildReplyRoomSid(  ):string {
		return com\Lib\Fun\Str::uuidKeyGen();
	}
	*/

	// ===============================
	//
	//
	// 下面为静态方法
	//
	//
	// ===============================

	/**
	 * @param com\Cla\EChat $eChat
	 * @param array $roomSids
	 * @return $this
	 */
	public static function makeOfNew( com\Cla\EChat $eChat, array $roomSids ):self {
	    return self::make( $eChat, $roomSids );
	}
	
}
